package com.innjia.app.controller;

import com.innjia.base.utils.PageUtils;
import com.innjia.base.utils.Query;
import com.innjia.base.utils.Result;
import com.innjia.sys.entity.ContractInfoEntity;
import com.innjia.sys.entity.UserEntity;
import com.innjia.sys.service.ContractInfoService;
import com.innjia.sys.service.HouseService;
import com.innjia.sys.service.UserAdminService;
import com.innjia.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("app/contractinfo")
public class AppContractInfoController {

    @Autowired
    private ContractInfoService contractInfoService;

    /**
     * app查询
     */
    @RequestMapping("/searchAppList")
    public Result searchAppList(@RequestParam Map<String, Object> params){
    	 UserEntity userEntity = ShiroUtils.getUserEntity();
    	 String phone = userEntity.getPhone();
    	 Date date = new Date();
    	 //更新该账户下已过签约时效的合同状态为已作废
         List<ContractInfoEntity> contractInfoEntities = contractInfoService.queryByTenantOrLandlordPhone(phone);
         if(contractInfoEntities != null && contractInfoEntities.size() > 0) {
        	 contractInfoEntities.forEach(contract -> {
        		 contract.setContractStatus("2");
        		 contract.setContractTerminationDate(date);
        		 contract.setUpdateTime(date);
                 contractInfoService.update(contract);
        	 });
         }
    	 
        if (params.get("role")!=null){ //判断角色是否为空
            Query query = new Query(params);
            if ("renter".equals(params.get("role"))){//判断是否为租客
                query.put("phone",phone);

                //Count值作为分页用
                int total = contractInfoService.searchCountAppTenantList(query);

                List<ContractInfoEntity> contractInfoList = contractInfoService.searchAppTenantList(query);

                contractInfoList = filterContractInfoEntity(contractInfoList);//过滤

                PageUtils pageUtil = new PageUtils(contractInfoList, total, query.getLimit(), query.getPage());

                return Result.ok().put("page", pageUtil);
            }

            //Count值作为分页用
            int total = contractInfoService.searchCountAppLandlordList(query);

            List<ContractInfoEntity> contractInfoList = contractInfoService.searchAppLandlordList(query);

            contractInfoList = filterContractInfoEntity(contractInfoList);//过滤

            PageUtils pageUtil = new PageUtils(contractInfoList, total, query.getLimit(), query.getPage());

            return Result.ok().put("page", pageUtil);
        }


        return Result.ok().put("page", null);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//	@RequiresPermissions("contractinfo:info")
    public Result info(@PathVariable("id") String id){
        ContractInfoEntity contractInfo = contractInfoService.queryObject(id);

        String tenantPhone = contractInfo.getTenantPhone();
        contractInfo.setTenantPhone(stringSubstring(tenantPhone));
        String landlordPhone = contractInfo.getLandlordPhone();
        contractInfo.setLandlordPhone(stringSubstring(landlordPhone));
        String tenantIdCard = contractInfo.getTenantIdCard();
        contractInfo.setTenantIdCard(stringCard(tenantIdCard));
        String landlordIdCard = contractInfo.getLandlordIdCard();
        contractInfo.setLandlordIdCard(stringCard(landlordIdCard));

        return Result.ok().put("contractInfo", contractInfo);
    }

    /**
     * 查询履行合约的记录数
     */
    @RequestMapping("/observant")
//	@RequiresPermissions("contractinfo:info")
    public Result observant(){
        Map<String,Object> hashMap = new HashMap();
        UserEntity userEntity = ShiroUtils.getUserEntity();
        hashMap.put("phone",userEntity.getPhone());

        int count = contractInfoService.dulfillmentOfContractRecords(hashMap);

        return Result.ok().put("count", count);
    }

    /**
     * 过滤手机号
     * @return
     */
    private List<ContractInfoEntity> filterContractInfoEntity(List<ContractInfoEntity> contractInfoList){
        for (ContractInfoEntity contractInfoEntity : contractInfoList) {
            String tenantPhone = contractInfoEntity.getTenantPhone();
            contractInfoEntity.setTenantPhone(stringSubstring(tenantPhone));
            String landlordPhone = contractInfoEntity.getLandlordPhone();
            contractInfoEntity.setLandlordPhone(stringSubstring(landlordPhone));
            String tenantIdCard = contractInfoEntity.getTenantIdCard();
            contractInfoEntity.setTenantIdCard(stringCard(tenantIdCard));
            String landlordIdCard = contractInfoEntity.getLandlordIdCard();
            contractInfoEntity.setLandlordIdCard(stringCard(landlordIdCard));
        }
        return contractInfoList;
    }


    /**
     * 支付宝格式化手机号
     * 12345678901 -- 123****8901
     * @param phone
     * @return
     */
    private String stringSubstring(String phone){

        return phone.substring(0, 3) + "****" + phone.substring(phone.length()-4, phone.length());
    }
   /**
     * 支付宝格式化身份证 只保留第一位和最后一位
     * 210111122221111111 -- 2****************1
     * @param carcd
     * @return
     */
    private  String stringCard(String carcd){
        if (carcd.length()==18){

            return carcd.substring(0, 1) + "****************" + carcd.substring(carcd.length()-1, carcd.length());

        }

        return carcd.substring(0, 1) + "*************" + carcd.substring(carcd.length()-1, carcd.length());
    }

}
